package cn.javablog.controller;


import cn.javablog.Vo.PageVo;
import cn.javablog.Vo.TypeVo;
import cn.javablog.common.lang.Result;
import cn.javablog.entity.TBlog;
import cn.javablog.entity.TType;
import cn.javablog.service.TBlogService;
import cn.javablog.service.TTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javafx.print.PageLayout;
import net.sf.saxon.expr.instruct.ForEach;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AllenYe
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/type")
public class TTypeController {

    @Autowired
    private TTypeService tTypeService;
    @Autowired
    private TBlogService tBlogService;
    @GetMapping("/lists")
    public Result typeLists(){
        Page page = new Page();
        QueryWrapper<TType> wrapper = new QueryWrapper();
//        wrapper.like("name", pageVo.getName());
        IPage pageList = tTypeService.page(page, wrapper);
        if(pageList.getTotal()>0){
            for(int i = 0; i < pageList.getTotal();i++){
                TType obj = (TType) pageList.getRecords().get(i);
//                Object obj = pageList.getRecords().get(i);
                QueryWrapper<TBlog> query = new QueryWrapper<TBlog>();
                query.eq("type_id",obj.getId());
                int count = tBlogService.count(query);
                Map<String,String> map = new HashMap<>();
                map.put("id",String.valueOf(obj.getId()));
                map.put("name",obj.getName());
                map.put("count",String.valueOf(count));
                pageList.getRecords().set(i,map);
            }
        }
        return Result.succ(pageList);
    }
    /**
     * 获取分类
     * @param pageVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/list")
    public Result typeList(@RequestBody PageVo pageVo){
        Page page = new Page(pageVo.getPageCurrent() == 0 ? 1 : pageVo.getPageCurrent(),pageVo.getPageSize() == 0 ? 10 : pageVo.getPageSize());
        QueryWrapper<TType> wrapper = new QueryWrapper();
        wrapper.like("name", pageVo.getName());
        IPage pageList = tTypeService.page(page, wrapper);
        return Result.succ(pageList);
    }

    /**
     * 添加分类
     * @param typeVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/add")
    public Result addType(@Validated @RequestBody TypeVo typeVo){
        TType ttype = tTypeService.getOne(new QueryWrapper<TType>().eq("name", typeVo.getName()));
        if(ttype != null){
            return  Result.fail("分类名称不能重复");
        }else{
            TType type1  = new TType().setName(typeVo.getName());
            boolean flag = tTypeService.save(type1);
            if(flag){
                return   Result.succ("添加成功",null);
            }else{
                return  Result.fail("添加失败",null);
            }
        }
    }

    /**
     * 更新分类
     * @param typeVo
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/edit")
    public Result editType(@Validated @RequestBody TypeVo typeVo){
        TType typeId = tTypeService.getOne(new QueryWrapper<TType>().eq("id", typeVo.getId()));
        Assert.notNull(typeId,"该分类不存在");
        TType typeName = tTypeService.getOne(new QueryWrapper<TType>().eq("name", typeVo.getName().toLowerCase()));
        if(typeName!=null){
            if(typeId.getId()!=typeName.getId()){
                Result.fail("分类名称不能重复");
            }
        }
        TType newType = new TType().setName(typeVo.getName());
        boolean editFlag = tTypeService.update(newType, new QueryWrapper<TType>().eq("id", typeVo.getId()));
        if(editFlag){
            return Result.succ("更新成功",null);
        }else{
            return Result.fail("更新失败",null);
        }
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @RequiresAuthentication
    @GetMapping(value = "/del")
    public Result typeDel(@RequestParam int id){
        TType delType = tTypeService.getOne(new QueryWrapper<TType>().eq("id", id));
        Assert.notNull(delType,"该分类不存在");
        boolean delFlag = tTypeService.removeById(id);
        if(delFlag){
            return Result.succ("删除成功",null);
        }else{
            return Result.fail("删除失败",null);
        }
    }
}
